﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using JEdwonTune.Lib.Crud;
using System.Xml;
using JEdwonTune.Lib.Reflect;
using JEdwonTune.Lib;
using JEdwonTune.Common;

namespace JEdwonTune.Crud {
    public partial class FrmImportDll : FrmXmlNodeEditorBase {
        public FrmImportDll() {
            InitializeComponent();
        }

        private String _FilePath = null;
        private BizDllEO _dllEO = null;
        private XmlNode _Models = null;
        private void btnOpen_Click(object sender, EventArgs e) {
            this.openFileDialog1.FileName = "";
            this.openFileDialog1.Filter = "程序集文件(*.dll)|*.dll";
            DialogResult dr = this.openFileDialog1.ShowDialog();
            if (dr == DialogResult.OK) {
                _FilePath = this.openFileDialog1.FileName;
                try {
                    _dllEO = RefHelper.ParseBizAssembly(_FilePath);
                    this.dataGridView1.DataSource = _dllEO.Entities;
                    this.txtInfo.Text = _dllEO.Assem.FullName;
                } catch (Exception ex) {
                    MessageBox.Show(ex.Message, "读取文件错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

            }
        }

        private void btnOK_Click(object sender, EventArgs e) {
            if (_dllEO == null) {
                MessageBox.Show("没有加载程序集", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            DataGridViewSelectedRowCollection rows = this.dataGridView1.SelectedRows;
            if (rows.Count < 1) {
                MessageBox.Show("没有选择任何实体", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            foreach (DataGridViewRow row in rows) {
                BuildModelNode(row.Index);
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void BuildModelNode(Int32 index) {
            EntityEO entity = _dllEO.Entities[index];
            XmlNode model = Doc.CreateElement("model");
            SetNodeAttrValue(model, "name", entity.Type.FullName);
            SetNodeAttrValue(model, "type", entity.Type.FullName + "," + _dllEO.Assem.FullName.Split(',')[0]);
            PropertyInfo[] pis = entity.Type.GetProperties();
            foreach (PropertyInfo pi in pis) {
                XmlNode property = Doc.CreateElement("property");
                SetNodeAttrValue(property, "name", pi.Name);
                SetNodeAttrValue(property, "type", pi.PropertyType.Name);
                property.InnerXml = "<![CDATA["+pi.Name+"]]>";
                model.AppendChild(property);
            }
            _Models.AppendChild(model);
        }

        private void FrmImportDll_Load(object sender, EventArgs e) {
            if (Doc == null) {
                this.btnOpen.Enabled = false;
                this.btnOK.Enabled = false;
                this.txtInfo.Text = "初始化DOC错误";
                return;
            }
            String ns = Doc.DocumentElement.NamespaceURI;
            XmlNamespaceManager xnm = new XmlNamespaceManager(new NameTable());
            xnm.AddNamespace("jc",ns);
            _Models = Doc.SelectSingleNode("//jc:models",xnm);
            if (_Models == null) {
                this.btnOpen.Enabled = false;
                this.btnOK.Enabled = false;
                this.txtInfo.Text = "配置文件结构有错误，未找到models节点";
                return;
            }
            this.dataGridView1.AutoGenerateColumns = false;
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
